//XML_Generator.cpp
//**************************************************************************//
//																			//
//	XML Generator															//
//	Author : Taek han, Go(FreeMan ; http://tmvlemaos.egloos.com)			//
//	First Create : 2006-12-29												//
//	Last Editing : 2007-01-11												//
//	Function : Create the XML Document(RSS)							 		//
//																			//
//**************************************************************************//
#include "XML_Util.h"		//GetXMLDeclaration, CreateStartElement, CreateEndElement, CreateElement, CreateTag
#include "XML_Generator.h"	//RecordRSS, RecordItem
#include "Item.h"			//Item
#include "Tag.h"

#include <iostream>
#include <cstdio>			//file processing( fopen, fprintf, fclose )
#include <iomanip>
#include <mysql++.h>

using namespace mysqlpp;
using namespace std;

//////////////////////////////////////////////////////////////////////
// RecordRSS							            				//
//////////////////////////////////////////////////////////////////////
void RecordRSS( FILE* rss, string filename, int itemCount, Item* itemSet, Tag* keyWord )
{
	//Declaration XML_Util Variable
	XML_Util xml;
	
	//Declaration string Variable
	string buffer;

	//filename.append( ".xml" );
	//Open File
	rss = fopen( const_cast<char*>( filename.c_str() ), "wt" );

	
	//////////////////////////////////////////////////////////////////////////////////////
	//	Create XMl Document Declaration Part											//
	//////////////////////////////////////////////////////////////////////////////////////
	
	buffer = xml.GetXMLDeclaration( "1.0", "utf-8" );
	fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

	buffer = xml.CreateStartElement( "rss", "version=\"2.0\"" );
	fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

	buffer = xml.CreateElement( "channel", "" );
	fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

	//////////////////////////////////////////////////////////////////////////////////////
	//	Create Channel Information														//
	//////////////////////////////////////////////////////////////////////////////////////
	
	//channel >> title
	buffer = xml.CreateTag( "title", "", keyWord->GetTitle() );
	fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

	//channel >> link
	buffer = xml.CreateTag( "link", "", "http://uccviewer.tistory.com/" );
	fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

	// Descrption >> <description>
	buffer = xml.CreateStartElement( "description", "");
	fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

	// Description >> CDATA
	buffer = xml.CreateCDATASection ( "Portable UCC Browser Server RSS" );
	fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

	// Description >> </description>
	buffer = xml.CreateEndElement("description");
	fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );
	
	/*
	//channel >> description
	buffer = xml.CreateTag( "description", "", "Portable UCC Viewer Server RSS" );
	fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

	*/

	//Call the RecordItem Module
	RecordItem( rss, itemCount, itemSet );

	fclose(rss);
}



//////////////////////////////////////////////////////////////////////
// RecordItem							    						//
//////////////////////////////////////////////////////////////////////
void RecordItem( FILE* rss, int itemCount, Item* itemSet )
{
	//Declaration XML_Util Variable
	XML_Util xml;

	//Declaration string Variable
	string buffer;

	//Loop for Creating Items
	for( int i = 0; i < itemCount; i++ )
	{
		//Create Start Element for Item
		buffer = xml.CreateStartElement( "item", "" );
		fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str()) );

		//<!------------- title ------------->
		buffer = xml.CreateTag("title", "", itemSet[i].GetTitle() );
		fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );
		
		//<!------------- link -------------->
		buffer = xml.CreateTag( "link", "", itemSet[i].GetLink() );
		fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

		//<!---------- description ---------->
		buffer = xml.CreateStartElement( "description", "" );
		fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );
	
		buffer = xml.CreateCDATASection( itemSet[i].GetDescription() );
		fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

		buffer = xml.CreateEndElement( "description" );
		fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );
	
		//<!------------ Author ------------->
		buffer = xml.CreateTag( "author", "", itemSet[i].GetAuthor()  );
		fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str()) );

		//<!------------ pubDate ------------>
		buffer = xml.CreateTag( "pubDate", "", string( (char*)( itemSet[i].GetPublishDate() ) ) );
		fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

		//<!------------ image -------------->
		string thumbnail = GetThumbnailImage( itemSet[i].GetLink() );

		int flag = thumbnail.length();

		if( flag > 0 )
		{
			buffer = xml.CreateTag( "img", "", thumbnail );
			fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );
		}
		else if( flag == 0 )
		{
			buffer = xml.CreateStartElement( "img", "" );
			fprintf( rss, "%s", const_cast<char*>( buffer.c_str() ) );

			buffer = xml.CreateEndElement( "img" );
			fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );
		}

		GetTags( rss, itemSet[i].GetLink() );		
		
		//<!------------ image -------------->

		//<!---------- Category(Tag) -------->
		#if 0
		cout << ">> Test Print: ItemCount:" << itemSet[i].GetCountOfTags() << endl;
		
		for ( int j = 0;  j < itemSet[i].GetCountOfTags(); j++ )
		{
			buffer = xml.CreateTag( "category", "", (itemSet[i][j])->GetTitle() );
			fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );
			cout << ">> Print Tag: " << buffer << endl;
		}
		#endif
		//<!---------- End of Item --------->
			
		//Create End Element for Item
		buffer = xml.CreateEndElement( "item" );
		fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );

	}//while end
		
	//Create End Element for Channel
	buffer = xml.CreateEndElement( "channel" );
	fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );
	
	//Create End Element for RSS
	buffer = xml.CreateEndElement( "rss" );
	fprintf( rss, "%s", const_cast<char*>( buffer.c_str() ) );

}

//GetThumbnailImage
string GetThumbnailImage( string itemLink )
{
	//Open the database
	Connection con( "rss", "localhost", "root", "dreams" );

	//Declaration Query Variable
	Query query = con.query();

	//Declaration String Variable
	string thumbnaiI = "";

	//Set Query Statement
	query << "select ThumbnailImage.pngImage, ThumbnailImage.localDirectory from ThumbnailImage where ThumbnailImage.itemLink=\""
		  << const_cast<char*>( itemLink.c_str() ) << "\"";

	string preview = query.preview();

	cout << "Preview : " << preview << endl;

	Result res = query.store();

	int count = res.num_rows();

	cout << "Count : " << count << endl;

	if( count > 0 )
	{
		Row row = res.at(0);

		thumbnaiI = string( row.raw_data(0) );

		char* temp = new char[1024];

		sprintf( temp, "cp .%s/%s ./", row.raw_data(1), row.raw_data(0) );

		system( temp );

		delete[] temp;
	}

	return thumbnaiI;	
}




//GetTags
void GetTags( FILE* rss, string itemLink )
{
	//Open the database
	Connection con( "rss", "localhost", "root", "dreams" );

	//Declaration Query Variable
	Query query = con.query();

	//Set Query Statement
	query << "select Tag_Item.title from Tag_Item where itemLink=\"" << const_cast<char*>( itemLink.c_str() ) << "\"";

	Result res = query.store();

	int count = res.num_rows();

	if( count > 0 )
	{
		XML_Util xml;		

		Row row;
		
		string buffer;
		
		if( count == 1 )
		{
			row = res.at(0);

			buffer = xml.CreateTag( "category", "", row.raw_data(0) );
			fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );
		}
		else
		{
			Row::size_type i;
			try {
				for( i=0; row = res.at(i); ++i )
				{
					buffer = xml.CreateTag( "category", "", row.raw_data(0) );
					fprintf( rss, "%s\n", const_cast<char*>( buffer.c_str() ) );
				}
			}
			catch( const EndOfResults& end )
			{
			}			
		}//End if
	}//End If
}
